package at.jaki.ubb.crawling.demo;

import java.util.ArrayList;
import java.util.List;

import at.jaki.ubb.crawling.demo.dataFinder.MediafaxSearcher;
import at.jaki.ubb.crawling.demo.dataFinder.RealitateaSearcher;
import at.jaki.ubb.crawling.demo.dataFinder.TargetSplitter;
import at.jaki.ubb.crawling.demo.dataTransformer.ArticleKeywordExtractor;
import at.jaki.ubb.crawling.demo.dataTransformer.MediafaxLinkExtractor;
import at.jaki.ubb.crawling.demo.dataTransformer.PageClassifier;
import at.jaki.ubb.crawling.demo.dataTransformer.RealitateaLinkExtractor;
import at.jaki.ubb.crawling.demo.db.Interpreter;
import at.jaki.ubb.crawling.demo.db.MockDbFacade;
import at.jaki.ubb.crawling.model.config.CrawlingEngineConfig;
import at.jaki.ubb.crawling.model.config.DataFinderConfig;
import at.jaki.ubb.crawling.model.config.DataFinderManagerConfig;
import at.jaki.ubb.crawling.model.config.DataTransformerConfig;
import at.jaki.ubb.crawling.model.config.DataTransformerManagerConfig;
import at.jaki.ubb.crawling.model.config.FilterElementConfig;
import at.jaki.ubb.crawling.model.config.TargetQueueConfig;
import at.jaki.ubb.crawling.stlib.dataFinder.BabylonTranslator;
import at.jaki.ubb.crawling.stlib.dataFinder.SimpleHttpClient;
import at.jaki.ubb.crawling.stlib.filters.UniqueTargetFilter;

public class CofingGenerator {

	public static CrawlingEngineConfig createMainConfig() {
		CrawlingEngineConfig cec = new CrawlingEngineConfig();
		DataFinderManagerConfig dataFinderManagerConfig = createDataFindersConfig();
		cec.setDataFinderManagerConfig(dataFinderManagerConfig);
		DataTransformerManagerConfig dataTransformerManagerConfig = createDataTransofmersConfig();
		cec.setDataTransformerManagerConfig(dataTransformerManagerConfig);
		TargetQueueConfig targetQueueConfig = createTargetQueueConfig();
		cec.setTargetQueueConfig(targetQueueConfig);

		cec.setDbFacadeClass(MockDbFacade.class.getCanonicalName());
		cec.setDataInterpreterClass(Interpreter.class.getCanonicalName());
		return cec;
	}

	private static TargetQueueConfig createTargetQueueConfig() {
		TargetQueueConfig targetQueueConfig = new TargetQueueConfig();
		List<FilterElementConfig> targetConfigList = new ArrayList<FilterElementConfig>();
		targetConfigList.add(getUniqueFilterConfig());
		targetQueueConfig.setTargetConfigList(targetConfigList);
		return targetQueueConfig;
	}

	private static FilterElementConfig getUniqueFilterConfig() {
		FilterElementConfig filterElementConfig = new FilterElementConfig();
		filterElementConfig.setClazzName(UniqueTargetFilter.class.getCanonicalName());
		filterElementConfig.setLogName("uniqueness filter");
		return filterElementConfig;
	}

	private static DataTransformerManagerConfig createDataTransofmersConfig() {
		DataTransformerManagerConfig dtmc = new DataTransformerManagerConfig();
		List<DataTransformerConfig> dataTransformers = new ArrayList<DataTransformerConfig>();
		dataTransformers.add(getWebPageClassifier());
		dataTransformers.add(getLinkArticleExtrector());
		dataTransformers.add(getMediafaxSearchPageTransformer());
		dataTransformers.add(getRealitateaSearchPageTransformer());
		dtmc.setDataTransformers(dataTransformers);
		return dtmc;
	}

	private static DataFinderManagerConfig createDataFindersConfig() {
		DataFinderManagerConfig dfmc = new DataFinderManagerConfig();
		List<DataFinderConfig> dataFinders = new ArrayList<DataFinderConfig>();
		dataFinders.add(getWebPageMiner());
		dataFinders.add(getTranslator());
		dataFinders.add(getMediafaxSearcher());
		dataFinders.add(getRealitateaSearcher());
		dataFinders.add(getTargetSplitter());
		dfmc.setDataFinders(dataFinders);
		return dfmc;
	}

	private static DataFinderConfig getMediafaxSearcher() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(MediafaxSearcher.class.getCanonicalName());
		dfc.setLogName("mediafax searcher");
		return dfc;
	}
	
	private static DataFinderConfig getRealitateaSearcher() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(RealitateaSearcher.class.getCanonicalName());
		dfc.setLogName("realitatea searcher");
		return dfc;
	}

	private static DataFinderConfig getTargetSplitter() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(TargetSplitter.class.getCanonicalName());
		dfc.setLogName("target splitter");
		return dfc;
	}

	private static DataFinderConfig getWebPageMiner() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(SimpleHttpClient.class.getCanonicalName());
		dfc.setLogName("webpage miner");
		return dfc;
	}

	private static DataFinderConfig getTranslator() {
		DataFinderConfig dfc = new DataFinderConfig();
		dfc.setClazzName(BabylonTranslator.class.getCanonicalName());
		dfc.setLogName("translator");
		return dfc;
	}

	private static DataTransformerConfig getWebPageClassifier() {
		DataTransformerConfig dtc = new DataTransformerConfig();
		dtc.setClazzName(PageClassifier.class.getCanonicalName());
		dtc.setLogName("page classifier");
		return dtc;
	}

	private static DataTransformerConfig getLinkArticleExtrector() {
		DataTransformerConfig dtc = new DataTransformerConfig();
		dtc.setClazzName(ArticleKeywordExtractor.class.getCanonicalName());
		dtc.setLogName("keyword extractor");
		return dtc;
	}

	private static DataTransformerConfig getMediafaxSearchPageTransformer() {
		DataTransformerConfig dtc = new DataTransformerConfig();
		dtc.setClazzName(MediafaxLinkExtractor.class.getCanonicalName());
		dtc.setLogName("mediafax search result parser");
		return dtc;
	}
	
	private static DataTransformerConfig getRealitateaSearchPageTransformer() {
		DataTransformerConfig dtc = new DataTransformerConfig();
		dtc.setClazzName(RealitateaLinkExtractor.class.getCanonicalName());
		dtc.setLogName("realitatea search result parser");
		return dtc;
	}

}
